package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Observable;

import javax.swing.AbstractButton;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import edu.fim.core.util.BmiCategories;
import edu.fim.presentation.controller.ConsumerController;


public class ProfileForm extends AbstractForm {

	private static final long serialVersionUID = -6443523291947515953L;
	
	private ConsumerController consumerController;
	private JLabel nameLabel;
	private JLabel ageLabel;
	private JLabel heightLabel;
	private JLabel weightLabel;
	private JLabel caloriesThresholdLabel;

	public ProfileForm(
			String titulo,
			ConsumerController consumerController) {
		super(titulo);
		initialize();
		this.consumerController = consumerController;
		update(null, null);
	}
	
	@Override
	public void update(Observable o, Object arg) {
		nameLabel.setText(consumerController.getCurrentConsumer().getNome());
		ageLabel.setText(consumerController.getCurrentConsumer().getIdade().toString());
		heightLabel.setText(consumerController.getCurrentConsumer().getAltura().toString());
		weightLabel.setText(consumerController.getCurrentConsumer().getPeso().toString());
		caloriesThresholdLabel.setText(consumerController.getCurrentConsumer().getLimiarCalorias().toString());
		pack();
	}

	protected void initializeDynamicComponents() {
		nameLabel = new JLabel();
		ageLabel = new JLabel();
		heightLabel = new JLabel();
		weightLabel = new JLabel();
		caloriesThresholdLabel = new JLabel();
	}

	protected void buildLayout() {
		GroupLayout layout = new GroupLayout(getContentPane());
		
		Container containerDados = criarContainerDados();
		Component componenteCalculoIMC = criarComponenteCalculoIMC();
		Component componenteRetornoTela = criarComponenteRetornoTela();
		
		layout.setHorizontalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(containerDados)
						.addGroup(layout.createSequentialGroup()
						.addComponent(componenteCalculoIMC)
						.addComponent(componenteRetornoTela))));
		
		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addComponent(containerDados)
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(componenteCalculoIMC)
						.addComponent(componenteRetornoTela)));
		
		getContentPane().setLayout(layout);
	}

	private Container criarContainerDados() {
		Container container = new Container();
		GroupLayout layout = new GroupLayout(container);
		
		JLabel nome = new JLabel("Name:");
		JLabel idade = new JLabel("Age:");
		JLabel altura = new JLabel("Height:");
		JLabel peso = new JLabel("Weight:");
		JLabel limiarCalorias = new JLabel("Calories Threshold:");
		Component componenteAlteracaoLimiar = criarComponentAlteracaoLimiar();

		layout.setHorizontalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(nome)
						.addComponent(idade)
						.addComponent(altura)
						.addComponent(peso)
						.addComponent(limiarCalorias))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
						.addComponent(nameLabel)
						.addComponent(ageLabel)
						.addComponent(heightLabel)
						.addComponent(weightLabel)
						.addComponent(caloriesThresholdLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(componenteAlteracaoLimiar)));
		
		layout.setVerticalGroup(
				layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(nome)
						.addComponent(nameLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(idade)
						.addComponent(ageLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(altura)
						.addComponent(heightLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(peso)
						.addComponent(weightLabel))
				.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(limiarCalorias)
						.addComponent(caloriesThresholdLabel)
						.addComponent(componenteAlteracaoLimiar)));
		container.setLayout(layout);
		
		return container;
	}

	private Component criarComponentAlteracaoLimiar() {
		AbstractButton componenteAlteracaoLimiar = new JButton(
				"Change Threshold");
		componenteAlteracaoLimiar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteAlteracaoLimiarActionPerformed(evt);
			}
		});
		return componenteAlteracaoLimiar;
	}
	
	private Component criarComponenteCalculoIMC() {
		AbstractButton componenteCalculoIMC = new JButton("My BMI");
		componenteCalculoIMC.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteCalculoIMCActionPerformed();
			}
		});
		return componenteCalculoIMC;
	}

	private Component criarComponenteRetornoTela() {
		AbstractButton componenteRetornoTela = new JButton("Back");
		componenteRetornoTela.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				componenteRetornoTelaActionPerformed(evt);
			}
		});
		return componenteRetornoTela;
	}

	private void componenteAlteracaoLimiarActionPerformed(ActionEvent evt) {
		String novoLimiarTexto = JOptionPane.showInputDialog(this,
				"Type the new calories threshold: ");
		if (novoLimiarTexto != null) {
			Double newThreshold = Double.parseDouble(novoLimiarTexto);
			try {
				consumerController.setCaloriesThreshold(newThreshold);
			}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			consumerController.getCurrentConsumer().notifyObservers();
			showMessage("Change successful.");
		}
	}
	
	private void componenteCalculoIMCActionPerformed() {
		BmiCategories bmiCategory = consumerController.determineBmi();
		showMessage("Your BMI is: " + bmiCategory.toString());
	}

	private void componenteRetornoTelaActionPerformed(ActionEvent evt) {
		this.setVisible(false);
	}
}